<template>
  <div class="app-container">
    <!-- 表单查询 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-select v-model="searchObj.mid" clearable placeholder="请选择分类">
        <el-option
          v-for="item in metas"
          :key="item.mid"
          :label="item.name"
          :value="item.mid"
        >
        </el-option>
      </el-select>

      <el-select v-model="searchObj.status" clearable placeholder="请选择状态">
        <el-option
          v-for="item in selectStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-select v-model="searchObj.ctype" clearable placeholder="请选择方式">
        <el-option
          v-for="item in selectCtype"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <div style="margin-top: 20px">
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="searchObj.createTimeBegin"
            type="datetime"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-form-item label="结束时间">
          <el-date-picker
            v-model="searchObj.createTimeEnd"
            type="datetime"
            placeholder="选择截止时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="getList()"
          >查询</el-button
        >
        <el-button type="default" @click="resetData()">清空</el-button>
      </div>
      <!-- 工具条 -->
      <div>
        <el-button type="danger" size="mini" @click="removeRows()"
          >批量删除</el-button
        >
        <el-button type="primary" size="mini" @click="adoptRows()"
          >批量通过</el-button
        >
      </div>
    </el-form>
    <!-- 复选框 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    />
    <!-- banner列表 -->
    <el-table
      :data="list"
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <!-- 复选框 -->
      <el-table-column type="selection" width="55" />

      <el-table-column type="index" width="50" label="序号" />
      <!-- <el-table-column prop="cid" label="cid" /> -->

      <el-table-column prop="param.mName" width="100" label="分类名" />
      <el-table-column prop="param.uName" width="150" label="用户名" />
      <el-table-column prop="param.uMail" width="160" label="邮箱" />

      <el-table-column prop="content" width="300" label="内容" />

      <el-table-column label="状态" width="80">
        <template slot-scope="scope">{{
          scope.row.status === 0
            ? "审核"
            : scope.row.status === 1
            ? "正常"
            : "下架"
        }}</template>
      </el-table-column>

      <el-table-column label="方式" width="80">
        <template slot-scope="scope">{{
          scope.row.ctype === 0
            ? "文字"
            : scope.row.ctype === 1
            ? "图片"
            : "视频"
        }}</template>
      </el-table-column>

      <el-table-column prop="createTime" label="发布时间" />

      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="removeDataById(scope.row)"
            >删除</el-button
          >
          <router-link :to="'/contentSet/edit/' + scope.row.cid">
            <el-button
              style="margin-left: 10px"
              type="text"
              size="small"
              icon="el-icon-edit"
              >编辑</el-button
            >
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      :page-sizes="[5, 10, 20, 50]"
      style="padding: 30px 0; text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="getList"
       @size-change="changeSize"
    />
  </div>
</template>


<script>
//引入接口定义的js文件
import contentSet from "@/api/contentSet";
import metaSet from "@/api/metaSet";

export default {
  //定义变量和初始值
  data() {
    return {
      current: 1, //当前页
      limit: 10, //每页显示记录数
      searchObj: {}, //条件封装对象
      metas: [], //分类集合
      list: [], //每页数据集合
      total: 0, //总记录数
      multipleSelection: [], // 批量选择中选择的记录列表
      selectStatus: [
        {
          value: "0",
          label: "审核",
        },
        {
          value: "1",
          label: "正常",
        },
        {
          value: "2",
          label: "下架",
        },
      ],
      selectCtype: [
        {
          value: "0",
          label: "文字",
        },
        {
          value: "1",
          label: "图片",
        },
        {
          value: "2",
          label: "视频",
        },
      ],
    };
  },
  created() {
    //在页面渲染之前执行
    //一般调用methods定义的方法，得到数据
    this.getList();
    this.getMetaAll();
  },
  //定义方法，进行请求接口调用
  methods: {
    // 重置查询表单
    resetData() {
      console.log("重置查询表单");
      this.searchObj = {};
      this.getList();
    },
    //获取选择复选框的id值
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
      console.log(selection);
    },

    getMetaAll() {
      metaSet.getAll().then((response) => {
        this.metas = response.data;
      });
    },

    // 当页码发生改变的时候
    changeSize(size) {
      this.limit = size;
      this.getList(1);
    },
    //用户设置列表
    getList(page = 1) {
      //添加当前页参数
      this.current = page;
      contentSet
        .getContentList(this.current, this.limit, this.searchObj)
        .then((response) => {
          //请求成功response是接口返回数据
          //返回集合赋值list
          this.list = response.data.records;
          //总记录数
          this.total = response.data.total;
          // console.log(this.list);
        })
        .catch((error) => {
          //请求失败
          console.log("失败" + error);
        });
    },
    //删除的方法
    removeDataById(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确定执行then方法
          //调用接口
          contentSet.delete(id).then((response) => {
            //提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //刷新页面
            this.getList(1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    removeRows() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          type: "warning",
          message: "请选择数据!",
        });
        return;
      }

      this.$confirm("是否批量删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //确定执行then方法
        var idList = [];
        //遍历数组得到每个id值，设置到idList里面
        for (var i = 0; i < this.multipleSelection.length; i++) {
          var obj = this.multipleSelection[i];
          var id = obj.cid;
          idList.push(id);
        }
        //调用接口
        contentSet.removeRows(idList).then((response) => {
          //提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //刷新页面
          this.getList(1);
        });
      });
    },
    //批量通过
    adoptRows() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          type: "warning",
          message: "请选择数据!",
        });
        return;
      }

      this.$confirm("是否批量审核为通过?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //确定执行then方法
        var idList = [];
        //遍历数组得到每个id值，设置到idList里面
        for (var i = 0; i < this.multipleSelection.length; i++) {
          var obj = this.multipleSelection[i];
          var id = obj.cid;
          idList.push(id);
        }
        //调用接口
        contentSet.adoptRows(idList).then((response) => {
          //提示
          this.$message({
            type: "success",
            message: "通过成功!",
          });
          //刷新页面
          this.getList(1);
        });
      });
    },
  },
};
</script>
